package com.example.ttt.controller;

import com.example.ttt.functions.*;
import com.example.ttt.jcomp.JFrameComponent;
import com.example.ttt.utils.OSSClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/*
 * 首页界面绘制
 * */
@Component
@Slf4j
public class GUIHome {

    @Autowired
    private JFrameComponent jFrameComponent;
    @Autowired
    private Button_LeftSelectedEvent button_leftSelectedEvent;
    @Autowired
    private Scanner_Music scanner_music;
    @Autowired
    private DoubleClickPlay doubleClickPlay;
    @Autowired
    private Button_StartOrStop buttonStartOrStop;
    @Autowired
    private Button_UpdateAvatar button_updateAvatar;
    @Autowired
    private OSSClient ossClient;

    public void init(){
        JFrame jFrame = jFrameComponent.getJFrame();
        jFrame.setVisible(true);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        String path = GUIHome.class.getClassLoader().getResource("static/R-C.jpg").getPath();
        if (path.substring(0,5).equals("file:")){
            path = path.substring(6,path.length());
        }
        jFrame.setIconImage(new ImageIcon(path).getImage());
        // 初始化填充表格数据
        scanner_music.scannerMusic();
    }
    public void home(){

        JFrame jFrame = jFrameComponent.getJFrame();
        JPanel contentPane = jFrameComponent.getContentPane();
        JDesktopPane leftdesktopPane = jFrameComponent.getLeftdesktopPane();

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setBounds(0, 0, 980, 606);
        contentPane.setBackground(UIManager.getColor("ComboBox.buttonHighlight"));
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        jFrame.setContentPane(contentPane);
        contentPane.setLayout(null);

        //左边的panel
        leftdesktopPane = new JDesktopPane();
        leftdesktopPane.setBounds(0, 0, 200, 497);
        leftdesktopPane.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
        contentPane.add(leftdesktopPane);

        // 设置头像，将头像进行尺寸和展示样式的转换
        JLabel avatar = jFrameComponent.getAvatar();
        avatar.setHorizontalAlignment(SwingConstants.CENTER);
        String path = GUIHome.class.getClassLoader().getResource("static/icon.png").getPath();
        if (path.substring(0,5).equals("file:")){
            path = path.substring(6,path.length());
        }
        log.info("初始头像加载地址：{}",path);
        ImageIcon imageIcon = new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(90, 90, Image.SCALE_SMOOTH));
        avatar.setIcon(imageIcon);
        avatar.setBounds(10, 10, 90, 90);
        leftdesktopPane.add(avatar);
        avatar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1){
                    button_updateAvatar.UpdateAvatar();
                }
            }
        });

        //更多音乐
        JLabel moreMusic = jFrameComponent.getMoreMusic();
        moreMusic.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/tianqi_4.png").getPath()));
        moreMusic.setHorizontalAlignment(SwingConstants.LEFT);
        moreMusic.setBounds(10, 269, 180, 43);
        moreMusic.setOpaque(true);
        // 事件监听
        moreMusic.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1){
                    button_leftSelectedEvent.selectedEvent(moreMusic);
                }
            }
        });
        leftdesktopPane.add(moreMusic);

        // 我的歌单
        JLabel myPlaylist = jFrameComponent.getMyPlaylist();
        myPlaylist.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/ziliao_3.png").getPath()));
        myPlaylist.setOpaque(true);
        myPlaylist.setHorizontalAlignment(SwingConstants.LEFT);
        myPlaylist.setBounds(10, 216, 180, 43);
        // 事件监听
        myPlaylist.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1){
                    button_leftSelectedEvent.selectedEvent(myPlaylist);
                }
            }
        });
        leftdesktopPane.add(myPlaylist);

        //我的喜欢
        JLabel myLiking = jFrameComponent.getMyLiking();
        myLiking.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/dianzan_2.png").getPath()));
        myLiking.setOpaque(true);
        myLiking.setHorizontalAlignment(SwingConstants.LEFT);
        myLiking.setBounds(10, 163, 180, 43);
        // 事件监听
        myLiking.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1){
                    button_leftSelectedEvent.selectedEvent(myLiking);
                }
            }
        });
        leftdesktopPane.add(myLiking);

        //本地音乐
        JLabel localMusic = jFrameComponent.getLocalMusic();
        localMusic.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/yinle_1.png").getPath()));
        localMusic.setOpaque(true);
        localMusic.setHorizontalAlignment(SwingConstants.LEFT);
        localMusic.setBounds(10, 115, 180, 43);
        localMusic.setBackground(Color.ORANGE);
        // 事件监听
        localMusic.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1){
                    button_leftSelectedEvent.selectedEvent(localMusic);
                }
            }
        });
        leftdesktopPane.add(localMusic);

        //下面的底部面板
        JDesktopPane buttonDesktopPane = jFrameComponent.getButtonDesktopPane();
        buttonDesktopPane.setBounds(0, 500, 980, 106);
        buttonDesktopPane.setBorder(new MatteBorder(1, 0, 0, 0, (Color) SystemColor.control));
        buttonDesktopPane.setBackground(Color.WHITE);
        contentPane.add(buttonDesktopPane);

        //上一曲
        JLabel prevsionSong = jFrameComponent.getPrevsionSong();
        prevsionSong.setBounds(20, 20, 30, 30);
        buttonDesktopPane.add(prevsionSong);
        prevsionSong.setHorizontalAlignment(SwingConstants.CENTER);
        prevsionSong.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/back_left.png").getPath()));

        //下一曲
        JLabel nextSong = jFrameComponent.getNextSong();
        nextSong.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/back_right.png").getPath()));
        nextSong.setHorizontalAlignment(SwingConstants.CENTER);
        nextSong.setBounds(187, 20, 30, 30);
        buttonDesktopPane.add(nextSong);

        //播放  暂停
        JLabel playerStop = jFrameComponent.getPlayerStop();
        playerStop.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/restart.png").getPath()));
        playerStop.setHorizontalAlignment(SwingConstants.CENTER);
        playerStop.setBounds(100, 13, 45, 45);
        // 事件监听
        playerStop.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
            }
        });
        buttonDesktopPane.add(playerStop);
        playerStop.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2){
                    buttonStartOrStop.stopOrStart();
                }
            }
        });

        //滑动条
        JSlider slider = jFrameComponent.getSlider();
        slider.setBackground(Color.WHITE);
        slider.setValue(0);
        slider.setBounds(316, 34, 598, 26);
        buttonDesktopPane.add(slider);

        //播放音乐的题目
        JLabel mylab5 = jFrameComponent.getMylab5();
        mylab5.setBounds(325, 5, 257, 30);
        buttonDesktopPane.add(mylab5);

        //表格对象
        JTable table11 = jFrameComponent.getTable11();
        table11.getTableHeader().setVisible(false);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setPreferredSize(new Dimension(0, 0));
        table11.getTableHeader().setDefaultRenderer(renderer);
        table11.setBorder(null);
        table11.setRowHeight(40);
        //双击播放事件
        table11.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2){
                    doubleClickPlay.start(table11);
                }
            }
        });

        //中间的滚动列表
        JScrollPane scrollPane = jFrameComponent.getScrollPane();
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setBounds(210, 98, 754, 399);
        scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        scrollPane.setWheelScrollingEnabled(true);
        scrollPane.getVerticalScrollBar().setUI(null);
        contentPane.add(scrollPane);

        //随机播放
        JLabel shuffleplayback = jFrameComponent.getShuffleplayback();
        shuffleplayback.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/guanli_5.png")));
        shuffleplayback.setBounds(210, 51, 116, 32);
        contentPane.add(shuffleplayback);

        //批量操作
        JLabel bulkoperations = jFrameComponent.getBulkoperations();
        bulkoperations.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/shezhi_6.png")));
        bulkoperations.setBounds(350, 51, 106, 32);
        contentPane.add(bulkoperations);

        //音乐库
        JLabel musiclibrary = jFrameComponent.getMusiclibrary();
        musiclibrary.setFont(new Font("宋体", Font.BOLD, 14));
        musiclibrary.setBounds(210, 10, 116, 31);
        contentPane.add(musiclibrary);

        //扫描音乐
        JLabel scanmusic = jFrameComponent.getScanmusic();
        scanmusic.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/sousuo_7.png")));
        scanmusic.setBounds(490, 51, 106, 32);
        // 事件监听
        contentPane.add(scanmusic);

        //音乐文件夹位置设置
        JLabel musiclocation = jFrameComponent.getMusiclocation();
        musiclocation.setIcon(new ImageIcon(GUIHome.class.getClassLoader().getResource("static/wenjianjia_8.png")));
        musiclocation.setBounds(630, 51, 106, 32);
        //设置点击事件setMusicLocation
        contentPane.add(musiclocation);
    }
}
